﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace STRM_Test
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private Color[] CreateColorMap()
        {
            Color[] colorMap = new Color[16384];
            for (int i = 0; i < colorMap.Length; i++)
            {
                // Neue Farbe [-1,1]:
                double red = Math.Sin(i * 2 * Math.PI / 2500d - Math.PI);
                double green = Math.Sin(i * 2 * Math.PI / 2500d - Math.PI / 2);
                double blue = Math.Sin(i * 2 * Math.PI / 2500d);

                // Neue Farbe [0,255]:
                red = (red + 1) * 0.5 * 255;
                green = (green + 1) * 0.5 * 255;
                blue = (blue + 1) * 0.5 * 255;

                Color newColor = Color.FromRgb(
                    (byte)red,
                    (byte)green,
                    (byte)blue);
                colorMap[i] = newColor;
            }

            return colorMap;
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Color[] ColorMap = CreateColorMap();

            System.Diagnostics.Debug.WriteLine("Open...");
            //String filePath = @"C:\Users\Nicolas\Desktop\STRM\entpackt\binSRTM.dat";
            String filePath = @"D:\Archiv\STRM\entpackt\binSRTM.dat";

            SrtmReader test = new SrtmReader(filePath, ColorMap);

            System.Diagnostics.Debug.WriteLine("Write JPG...");
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            Guid photoID = System.Guid.NewGuid();
            //String photolocation = @"C:\Users\Nicolas\Desktop\STRM\entpackt\binSRTM.jpg";  //file name  
            String photolocation = @"D:\Archiv\STRM\entpackt\binSRTM.jpg";  //file name  

            encoder.QualityLevel = 98;
            encoder.Frames.Add(BitmapFrame.Create(test.OutImg));

            using (var filestream = new FileStream(photolocation, FileMode.Create))
                encoder.Save(filestream);

            image1.Source = test.OutImg;

            System.Diagnostics.Debug.WriteLine("Ready");

        }
    }
}
